<?php
namespace Controller\Wcc;

use Controller\BaseController;
use Hoa\Registry\Registry;
use Library\Input;
use Library\Log;
use Library\View;
use Requests;


/**
 * OrderController
 * @author los_gsy
 */
class OrderController extends BaseController {
	/**
	 * 构造函数
	 */
	public function __construct() {
		parent::__construct();
	}


	/**
	 * test
	 */
	public function test() {
		echo __METHOD__, '<br />';
	}


	/**
	 * 下单
	 */
	public function placeOrder() {
		//定义变量
		$conf = Registry::get('conf');
		$wcc_fw_order = $conf['wcc']['fw_order'];
		$now = time();

		//http接口参数
		$url = $wcc_fw_order['url']['place_order'];
		$header = ['Accept' => 'application/json'];
		$option = [];

		$data = [];
		$data['uid'] = mt_rand(10000, 20000);
		$data['oilstation_id'] = mt_rand(10000, 20000);
		$data['shipping_method_id'] = mt_rand(1, 3);
		$data['remark'] = 'remark' . $data['uid'];
		$status_list = [10, 300];
		$data['status'] = $status_list[mt_rand(0, 1)];
		$data['split_reason'] = '';
		if (300 == $data['status']) {
			$data['pid'] = mt_rand(10000, 20000);
			$data['split_reason'] = 'split_reason' . $data['pid'];
		}

		$prod = [];
		$prod['id'] = mt_rand(10000, 20000);
		$prod['type'] = mt_rand(1, 3);
		$prod['name'] = 'name'. $prod['id'];
		$prod['attr'] = 'name' . $prod['id'];
		$prod['price'] = floatval(mt_rand(100, 1000)) / 100;
		$prod['qty'] = floatval(mt_rand(100, 1000)) / 100;
		$data['prod_list'] = [$prod];

		$invoice['type'] = mt_rand(1, 2);
		$invoice['title'] = 'title' . $invoice['type'];
		$invoice['content'] = 'content' . $invoice['type'];
		$data['invoice'] = $invoice;

		$pay = [];
		$pay['market_id'] = mt_rand(1, 5000);
		$pay['method'] = mt_rand(1, 3);
		$pay['total_amt'] = floatval(mt_rand(100, 10000)) / 100;
		$pay['orig_prod_amt'] = floatval(mt_rand(100, 10000)) / 100;
		$pay['prod_amt'] = floatval(mt_rand(100, 10000)) / 100;
		$pay['freight'] = floatval(mt_rand(100, 10000)) / 100;
		$pay['real_freight'] = floatval(mt_rand(100, 10000)) / 100;
		$pay['payable_amt'] = floatval(mt_rand(100, 10000)) / 100;
		$data['pay'] = $pay;

		//执行远程调用
		$resp = Requests::post($url, $header, $data, $option);
		$result = json_decode($resp->body, true);
		var_export($result);
		echo '<br />';
		var_export($resp);
	}


	/**
	 * 取消订单
	 */
	public function cancelOrder() {
		//获取参数
		$id = Input::get('id/d', 0);

		//定义变量
		$conf = Registry::get('conf');
		$wcc_fw_order = $conf['wcc']['fw_order'];

		//http接口参数
		$url = $wcc_fw_order['url']['cancel_order'];
		$header = ['Accept' => 'application/json'];
		$option = [];

		$data = [];
		$data['id'] = $id;

		//执行远程调用
		$resp = Requests::post($url, $header, $data, $option);
		$result = json_decode($resp->body, true);
		//$res = Requests::post($url, $data);
		var_export($result);
		echo '<br />';
		var_export($resp);
	}


	/**
	 * 支付订单
	 */
	public function payOrder() {
		//获取参数
		$id = Input::get('id/d', 0);
		$pay_amt = Input::get('pay_amt/f', 0);

		//定义变量
		$conf = Registry::get('conf');
		$wcc_fw_order = $conf['wcc']['fw_order'];

		//http接口参数
		$url = $wcc_fw_order['url']['pay_order'];
		$header = ['Accept' => 'application/json'];
		$option = [];

		$data = [];
		$data['id'] = $id;
		$data['pay_amt'] = $pay_amt;

		//执行远程调用
		$resp = Requests::post($url, $header, $data, $option);
		$result = json_decode($resp->body, true);
		var_export($result);
		echo '<br />';
		var_export($resp);
	}


	/**
	 * 查询订单
	 */
	public function getOrder() {
		//获取参数
		$id = Input::get('id/d', 0);

		//定义变量
		$conf = Registry::get('conf');
		$wcc_fw_order = $conf['wcc']['fw_order'];

		//http接口参数
		$url = $wcc_fw_order['url']['get_order'];
		$header = ['Accept' => 'application/json'];
		$option = [];

		$data = [];
		$data['id'] = $id;
		$url .= '?' . http_build_query($data);

		//执行远程调用
		$resp = Requests::get($url, $header, $option);
		$result = json_decode($resp->body, true);
		var_export($result);
// 		echo '<br />';
// 		var_export($resp);
	}








}